package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"hello/models"
	"math"
	"path"
	"strconv"
	"time"
)

type ArticleController struct {
	beego.Controller
}
//处理下拉框的请求
//RelatedSel指定关联表
//	o.QueryTable("Article").RelatedSel("ArticleType").Filter("ArticleType_TypeName",typeNmae).All(&articles)



func (this *ArticleController) ShowArticleList() {
	o := orm.NewOrm()
	/**
	querySeter调用函数，返回的还是自身，所以可以连环的向下调用

	其常用方法：
	All()
	One()
	Filter()
	Limit()
	OrderBy()
	Distinct()
	Count()
	Delete()
	Update()
	RelatedSel()
	Values()
	ValuesList()
	ValuesFlat()
	*/
	querySeter := o.QueryTable("Article")
	var articles []models.Article
	//num 数据总条数
	//num, err := querySeter.All(&articles)
	//_, err := querySeter.All(&articles)
	//if err != nil {
	//	return
	//}


	pageSize:=2
	pageIndex, err := strconv.Atoi(this.GetString("pageIndex"))
	//获取不到，则pageIndex为0
	if err!=nil {
		pageIndex=1
	}
	start:=pageSize*(pageIndex-1)
	//参数一：一页显示多少，start是起始查询位置
	querySeter.Limit(pageSize,start).All(&articles)
	//beego.Info(articles[0].Title,num)
	count, _ := querySeter.Count()//返回数据条目数


	//获取类型
	var types []models.ArticleType
	o.QueryTable("ArticleType").All(&types)
	this.Data["types"]=types
	this.Data["count"] = count
	this.Data["articles"] = articles

	pageCount:=math.Ceil(float64(count)/float64(pageSize))//向上取整
	this.Data["pageCount"] = pageCount
	this.Data["pageIndex"] = pageIndex
	this.TplName = "index.html"
}


func (this *ArticleController) ShowAddArticle() {
	this.TplName = "add.html"
}
func (this *ArticleController) HandleAddArticle() {
	content := this.GetString("content")
	articleName := this.GetString("articleName")

	//获取上传的静态文件:分别是file  header  error
	f, h, err := this.GetFile("uploadname")
	defer f.Close() //关闭上传上来的字节流

	if err != nil {
		beego.Info("上传文件失败!")
		return
	}

	ext := path.Ext(h.Filename) //后缀名
	//重命名
	//fileName := time.Now().Format("2006-01-02 15:04:05")

	//windows下文件名不能存在:所以需要适当修改
	fileName := time.Now().Format("2006-01-02 15-04-05")

	//h.Filename
	//h.Size
	//把浏览器上传的静态文件存储
	beego.Info("/static/img/" + h.Filename)

	//在linux上面"/static/img/"+h.Filename可用，但是windows上面"static/img/"+h.Filename才行，否则保存找不到路径
	//err1:=this.SaveToFile("uploadname","static/img/"+h.Filename)
	err1 := this.SaveToFile("uploadname", "static/img/"+fileName+ext)
	if err1 != nil {
		beego.Info("错误信息", err1)
	}
	//beego.Info(content,articleName,h.Filename)

	//插入数据
	o := orm.NewOrm()
	article := models.Article{}
	article.Title = articleName
	article.Content = content
	article.Img = "/static/img/" + fileName + ext
	_, err2 := o.Insert(&article)
	if err2 != nil {
		beego.Info("错误处理", err2)
		return
	}

	this.Redirect("/ShowArticle", 302)

	//this.TplName="add.html"
}

func (this *ArticleController) ShowArticleDetail() {
	//获取数据
	id, er := this.GetInt("articleId")
	//数据校验
	if er != nil {
		beego.Info("传递的链接错误")
	}
	//操作数据
	o := orm.NewOrm()
	var article models.Article
	article.Id = id

	o.Read(&article)

	//修改阅读量
	article.Count += 1
	o.Update(&article)

	//返回视图页面
	this.Data["article"] = article
	this.TplName = "content.html"
}


//删除文章
func (this *ArticleController)HandleDelete()  {
	id, _ := this.GetInt("id")
	o := orm.NewOrm()
	article := models.Article{Id: id}
	o.Delete(&article)

	this.Redirect("/ShowArticle",302)
}

//显示类型
func (this *ArticleController)ShowAddType()  {
	o := orm.NewOrm()
	var articleTypes[] models.ArticleType
	_, err := o.QueryTable("ArticleType").All(&articleTypes)
	if err!=nil {
		beego.Info("查询类型错误")
	}
	this.Data["types"]=articleTypes
	this.TplName="addType.html"
}
//增加类型
func (this *ArticleController)HandleAddType()  {
	typeName := this.GetString("typeName")
	o := orm.NewOrm()
	articleType := models.ArticleType{TypeName: typeName}
	_, err := o.Insert(&articleType)
	if err!=nil {
		beego.Info("插入失败")
		return
	}
	this.Redirect("/AddArticleType",302)
}